
import { _decorator, Component, Node, Button } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { ReactiveProperty } from '../../../../scripts/libs/rxjs/cc3/ReactiveProperty';
import { ProvideInfo } from '../../../../scripts/managers/SDK/IapSDKManager';
import { find } from 'cc';
import { MSResolve, MSRToString_th_m } from '../../../../scripts/libs/utils/date';
import { Label } from 'cc';
import { director } from 'cc';
import { v3 } from 'cc';
// import { LobbyPanel } from '../LobbyPanel/LobbyPanel';
import { ResourceHelper } from '../../ResourceHelper';
import { lm } from '../../LobbyManager';
import { JumpTo_2D, TweenToStart } from '../../../../scripts/libs/rxjs/cc3/TweenStart';
import { FlyItem } from '../../game/FlyItem';
import { ItemKey } from '../../../../scripts/managers/DevConfig';
import { concat, forkJoin } from 'rxjs';
import { instantFunc } from '../../../../scripts/libs/rxjs/cc3/CommonObservable';
import { Timer } from '../../../../scripts/libs/rxjs/cc3/CreatorSchedule';
import { UIOpacity } from 'cc';
import { UITransform } from 'cc';
import { nc } from '../../../../scripts/libs/rxjs/NotificationCenter';
const { ccclass, property } = _decorator;

@ccclass('GotAwardPanel')
export class GotAwardPanel extends BasePanel {

    data = ReactiveProperty.Create<ProvideInfo>(null);

    @property(Node)
    provideNode: Node = null;

    @property(Button)
    claimButton: Button = null;

    willBind(): void {
        this.provideNode.active = true;
    }

    useObserves() {
        return [
            BindButton(this.claimButton).subscribe(this.claimButtonClicked.bind(this)),
            this.data.subscribe(this.refresh.bind(this)),
        ];
    }

    claimButtonClicked() {
        this.flyProvide(this.data.value);
        this.provideNode.active = false;
        GotAwardPanel.CloseProcess(null, 'claim');
    }

    refresh() {
        if (this.data.value == null) return;
        let layoutNode = this.provideNode;
        for (let i = 0; i < layoutNode.children.length; i++) {
            let itemNode = layoutNode.children[i];
            if (this.data.value[itemNode.name] > 0) {
                itemNode.active = true;
                let valueLabelNode = find('normal/Label', itemNode);
                if (valueLabelNode != null) {
                    if (['inf_heart_sec', 'noads_sec'].includes(itemNode.name)) {
                        let msr = MSResolve(this.data.value[itemNode.name] * 1000);
                        valueLabelNode.getComponent(Label).string = MSRToString_th_m(msr);
                    } else {
                        valueLabelNode.getComponent(Label).string = `x${this.data.value[itemNode.name] ?? 0}`;
                    }
                }
            } else {
                itemNode.active = false;
            }
        }
    }

    flyProvide(provide: ProvideInfo) {
        if (provide != null) {
            let lobbyPanel = director.getScene().getComponentInChildren('LobbyPanel') as any;
            if (lobbyPanel?.node.isValid) {
                let homeNode = lobbyPanel.lobbyHomeButton.node;
                let homeWorldPos = v3(homeNode.worldPosition);
                // Item
                ['swapper', 'magnet', 'balloon'].forEach(itemKey => {
                    if (provide[itemKey] > 0) {
                        let nodeInProvideNode = this.provideNode.getChildByName(itemKey);
                        let iconNode = find('normal/icon', nodeInProvideNode);
                        let flyItemNode = ResourceHelper.FlyItemPool.getNode();
                        flyItemNode.setScale(1, 1, 1);
                        flyItemNode.getComponent(UIOpacity).opacity = 255;
                        flyItemNode.setParent(lm.uiRoot.tierFrontNode);
                        flyItemNode.setWorldPosition(iconNode.worldPosition);
                        flyItemNode.getComponent(FlyItem).itemKey.value = itemKey as ItemKey;
                        let targetLocalPos = v3();
                        lm.uiRoot.tierFrontNode.getComponent(UITransform).convertToNodeSpaceAR(homeWorldPos, targetLocalPos);
                        concat(
                            forkJoin([
                                // TweenToStart(flyItemNode, .6, { worldPosition: homeWorldPos }, { easing: 'quadInOut' }),
                                JumpTo_2D(flyItemNode, .8, targetLocalPos, 200, { easing: 'quadInOut' }),
                                concat(
                                    TweenToStart(flyItemNode, .3, { scale: v3(1.3, 1.3, 1.3) }, { easing: 'quadOut' }),
                                    TweenToStart(flyItemNode, .3, { scale: v3(1, 1, 1) }, { easing: 'quadIn' }),
                                ),
                                concat(
                                    Timer(.7),
                                    TweenToStart(flyItemNode.getComponent(UIOpacity), .1, { opacity: 0 }),
                                ),
                            ]),
                            instantFunc(() => {
                                ResourceHelper.FlyItemPool.putNode(flyItemNode);
                            })
                        ).subscribe()
                    }
                });
                ['coin'].forEach(itemKey => {
                    if (provide[itemKey] > 0) {
                        let nodeInProvideNode = this.provideNode.getChildByName(itemKey);
                        let iconNode = find('normal/icon', nodeInProvideNode);
                        nc.post('fly_coins', { fromWorldPos: iconNode.worldPosition, count: provide[itemKey] });
                    }
                });
            }
        }
    }

}